package com.cloudsigns.factorymanager2.engineering.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.engineering.bean.RegionBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tony on 2018/6/16.
 */

public class DropDownPoupWindowAdapter<T> extends BaseAdapter {

    private Context context;
    private List<T> datas = new ArrayList<T>();
    private LayoutInflater inflater;
    private String tag = "";

    public DropDownPoupWindowAdapter(Context context, String tag) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.tag = tag;
    }

    public void setData(List<T> list) {
        this.datas.clear();
        if (null != list) {
            this.datas.addAll(list);
        }

    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_drop_popwindow, null);
            holder.tvName = (TextView) convertView.findViewById(R.id.tv);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (!TextUtils.isEmpty(tag)) {
            if (tag.equals("PublicationStatusTableForManagerActivity2") || tag.equals("PatrolStatusTableForManagerActivity")) {
                RegionBean regionBean = (RegionBean) datas.get(position);
                if (null != regionBean)
                    holder.tvName.setText(regionBean.getRegionName());

            } else {
                String name = (String) datas.get(position);
                holder.tvName.setText(name);
            }
        } else {
            String name = (String) datas.get(position);
            holder.tvName.setText(name);
        }

        return convertView;
    }


    private class ViewHolder {
        private TextView tvName;
    }

}
